/*
 * Copyright (C) 2017 Qi Cai Technology Co., Ltd. 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package com.www.ww.login.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;

import com.example.lkslib.Utils.MetricUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

/**
 * TODO this class desription here
 * <p>
 * Created by ww on 2017/8/10.
 */
public class LoginDrawable extends Drawable {

    private TimerTask task;
    private Timer timer;
    private Bitmap bitmap;
    private RectF rectf;
    private Rect rect;//裁剪区域
    private BitmapRegionDecoder decoder;
    private Context mContext;
    int width;//原图的宽度
    int height;//原图的高度
    int offset;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==1){
                invalidateSelf();
            }
        }
    };

    public LoginDrawable(Context context) {
        this.mContext = context;
        init();
    }
    int windowHeight;
    int windowWith;
    int dexWidth;
    int direction=0;//0 向右 1向左
    private void init() {
        try {
             windowHeight = MetricUtil.getWindowHeight(mContext);
             windowWith = MetricUtil.getWindowWith(mContext);
            rectf=new RectF(0, 0, windowWith,windowHeight);
//            得到流
            InputStream inputStream = mContext.getAssets().open("icon_big_tiankong.jpg");
//            得到原图信息
            BitmapFactory.Options tmpOptions = new BitmapFactory.Options();
            tmpOptions.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(inputStream, null, tmpOptions);
            width = tmpOptions.outWidth;
            height = tmpOptions.outHeight;
            dexWidth = width - windowWith;
//            offset=dexWidth/3;
            rect=new Rect(offset,0, offset+windowWith,height);
            //得到裁剪的Bitmap
            decoder = BitmapRegionDecoder.newInstance(inputStream, false);
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            bitmap = decoder.decodeRegion(rect, options);
//            利用Timer来移动图片
            timer = new Timer();
            task = new TimerTask() {
                @Override
                public void run() {
                    if((offset+3)<dexWidth&&direction==0){
                        direction=0;
                    }else {
                        direction=1;
                    }

                    if(direction==0){
                        offset+=3;
                    }else if(direction==1){
                        offset-=3;
                    }
                    rect.left=offset;
                    rect.right=offset+windowWith;
                    bitmap = decoder.decodeRegion(rect, options);
                    handler.sendEmptyMessage(1);
                }
            };
            timer.schedule(task, 2000, 50);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void draw(Canvas canvas) {
        canvas.drawBitmap(bitmap, null, rectf, null);
    }

    @Override
    public void setAlpha(int alpha) {

    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {

    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSPARENT;
    }

}
